package com.fbs.wowo.forum.mapper;

import com.fbs.wowo.forum.entity.Comments;
import com.fbs.wowo.forum.vo.CommentsList;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface CommentsMapper {

    @Delete({
        "delete from comments",
        "where id = #{id,jdbcType=VARCHAR}"
    })
    int deleteByPrimaryKey(String id);

    @InsertProvider(type=CommentsSqlProvider.class, method="insertSelective")
    int insertSelective(Comments record);

    @Select({
        "select",
        "id, comment_entity, comment_type, comment_time, user_id, status, content",
        "from comments",
        "where id = #{id,jdbcType=VARCHAR}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.VARCHAR, id=true),
        @Result(column="comment_entity", property="commentEntity", jdbcType=JdbcType.VARCHAR),
        @Result(column="comment_type", property="commentType", jdbcType=JdbcType.INTEGER),
        @Result(column="comment_time", property="commentTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="user_id", property="userId", jdbcType=JdbcType.VARCHAR),
        @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
        @Result(column="content", property="content", jdbcType=JdbcType.LONGVARCHAR)
    })
    Comments selectByPrimaryKey(String id);

    @UpdateProvider(type=CommentsSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Comments record);

    /**
     * 某文章下的评论列表
     * 完整需求
     * 1. 分页
     * 2. 用户头像，名称 时间，发送者id
     * 3. 综合排序
     * 4. 能删除自己的评论
     * 5. 能点赞并取消点赞评论
     * 6. 能回复
     * 7. 能查看回复列表
     * 删减需求
     * 1. 分页
     * 2. 用户头像，名称
     * 3. 能删除自己的评论
     *
     * @param entityId 实体id
     * @param entityType 实体类型
     * @param status 状态
     * @return 评论列表
     */
    @Select({
            "select",
            "c.id, u.name, u.photo, c.user_id, c.content, c.comment_time",
            "from comments c",
            "left join user u",
            "on c.user_id=u.user_id",
            "where c.comment_entity = #{entityId,jdbcType=VARCHAR} and c.comment_type = #{entityType, jdbcType=VARCHAR} and c.status=#{status, jdbcType=INTEGER} order by c.comment_time desc"
    })
    @Results({
            @Result(column="id", property="commentId", jdbcType=JdbcType.VARCHAR, id=true),
            @Result(column="name", property="username", jdbcType=JdbcType.VARCHAR),
            @Result(column="photo", property="photo", jdbcType=JdbcType.VARCHAR),
            @Result(column="user_id", property="userId", jdbcType=JdbcType.VARCHAR),
            @Result(column="content", property="content", jdbcType=JdbcType.LONGVARCHAR),
            @Result(column="comment_time", property="commentTime", jdbcType=JdbcType.TIMESTAMP)
    })
    List<CommentsList> list(@Param("entityId") String entityId, @Param("entityType") Integer entityType, @Param("status") Integer status);

    /**
     * 获取评论数
     */
    @Select({
            "select",
            "count(id)",
            "from comments",
            "where comment_entity = #{entityId,jdbcType=VARCHAR} and comment_type = #{entityType, jdbcType=INTEGER}"
    })
    Integer count(@Param("entityId") String entityId, @Param("entityType") Integer entityType);

}